package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.domain.AjaxResult;
import com.itheima.common.domain.PageInfo;
import com.itheima.service.ISmsCouponHistoryService;
import com.itheima.sms.entity.SmsCouponHistory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/couponHistory")
public class SmsCouponHistoryController {

    @Autowired
    private ISmsCouponHistoryService smsCouponHistoryService;

    /**
     * 根据优惠券id，使用状态，订单编号分页获取领取记录
     *
     * @param couponId  优惠券id
     * @param orderSn   订单编号
     * @param pageNum
     * @param pageSize
     * @param useStatus 使用状态
     * @return
     */
    @GetMapping("/list")
    public AjaxResult list(@RequestParam(value = "couponId", required = false) Integer couponId,
                           @RequestParam(value = "orderSn", required = false) String orderSn,
                           @RequestParam(value = "pageNum", required = false) Integer pageNum,
                           @RequestParam(value = "pageSize", required = false) Integer pageSize,
                           @RequestParam(value = "useStatus", required = false) Integer useStatus) {
        Page<SmsCouponHistory> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SmsCouponHistory> lqw = new LambdaQueryWrapper<>();
        lqw.eq(couponId != null, SmsCouponHistory::getCouponId, couponId)
                .eq(orderSn != null, SmsCouponHistory::getOrderSn, orderSn)
                .eq(useStatus != null, SmsCouponHistory::getUseStatus, useStatus);
        smsCouponHistoryService.page(page, lqw);

        PageInfo pageInfo = PageInfo.toPageList(page);

        return AjaxResult.success(pageInfo);
    }
}
